﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using Microsoft.Practices.Unity;
using Microsoft.Practices.Unity.Configuration;
using Prisa.BussinesLogic;
using Prisa.Entities;

namespace Prisa.UI
{
    public partial class frmMttoCliente : Form
    {
        [Dependency]
        public IClienteBL cliBL { get; set; }

        bool isNew;

        public frmMttoCliente()
        {
            InitializeComponent();
            this.BuildUp();
        }      
        private void frmCliente_Load(object sender, EventArgs e)
        {
        }

        public CLIENTE CurrentCliente { get; set; }
        
        private void BtnAdd_Click(object sender, EventArgs e)
        {
            CLIENTE currentCliente = cLIENTEBindingSource.Current as CLIENTE;
            
            var msg = "ERROR";

            if (currentCliente != null)
            {
                currentCliente.NombreRazonSocial = nombreRazonSocialTextBox.Text.ToUpper();
                currentCliente.Direccion = direccionTextBox.Text.ToUpper();
                currentCliente.Email = emailTextBox.Text.ToUpper();
                currentCliente.Telefono = telefonoTextBox.Text.ToUpper();               
                if (isNew == true)
                {
                    try
                    {
                        cliBL.AddCliente(currentCliente);
                        MessageBox.Show("Registro Correcto", "Mensaje", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        Close(); 
                    }
                    catch (Exception ex)
                    {
	                    msg = ex.Message;
	                    MessageBox.Show(msg, "Mensaje", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                }
                if (isNew == false)
                {
                    try
                    {
                        cliBL.UpdateCliente(currentCliente);
                        MessageBox.Show("Modificación Correcta", "Mensaje", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        Close();
                    }
                    catch (Exception ex)
                    {
                        msg = ex.Message;
                        MessageBox.Show(msg, "Mensaje", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                }
            }           
        }

        public void AddCliente()
        {
            isNew = true;
            cLIENTEBindingSource.AddNew();
        }

        public void UpdateCliente()
        {
            isNew = false;
            cLIENTEBindingSource.DataSource = CurrentCliente;
            HabilitarTextBoxs();
        }

        private void BtnVerificarDniRuc_Click(object sender, EventArgs e)
        {
            if (dniRucTextBox.Text.Length > 0)
            {
                VerificarRuc();
                nombreRazonSocialTextBox.Focus();
            }
            else
            {
                MessageBox.Show(this, "El DNI-RUC es requerido", "Mensaje", MessageBoxButtons.OK, MessageBoxIcon.Information);
                dniRucTextBox.Focus();
            }
        }

        private void VerificarRuc()
        {
            #region Add
            if (isNew)
            {
                if (dniRucTextBox.Text.Length == 8 || dniRucTextBox.Text.Length == 11)
                {
                    var ExisteDNI = cliBL.GetFromOneClienteByDniRuc(dniRucTextBox.Text);
                    if (ExisteDNI != null)
                    {
                        cLIENTEBindingSource.DataSource = ExisteDNI;

                        DialogResult rpta = MessageBox.Show(this, string.Format("Número de DNI-RUC no disponible"), "Mensaje",
                            MessageBoxButtons.OK, MessageBoxIcon.Information);
                        if (rpta == DialogResult.OK)
                        {
                            Close();
                        }
                    }
                    else
                    {
                        MessageBox.Show(this, "Número de DNI-RUC disponible", "Mensaje", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        HabilitarTextBoxs();
                    }
                }
                else
                {
                    MessageBox.Show(this, "Longitud de DNI-RUC incorrecta", "Mensaje", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    InhabilitarTextBoxs();
                    dniRucTextBox.Focus();
                }
            }
            #endregion
            #region Update
            else
            {
                var _cliente = cLIENTEBindingSource.Current as CLIENTE;

                var existe = cliBL.ExisteDuplicidadDniRucAndRazonSocial(dniRucTextBox.Text, nombreRazonSocialTextBox.Text, _cliente.Id);
                if (existe)
                {
                    DialogResult rpta = MessageBox.Show(this, string.Format("Ya existe un Cliente con la misma información"),
                        "Mensaje", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    MessageBox.Show(this, "Cliente disponible", "Mensaje", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            #endregion
        }

        private void dniRucTextBox_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                if (dniRucTextBox.Text.Length > 0)
                {
                    VerificarRuc();
                    nombreRazonSocialTextBox.Focus();
                }
                else
                {
                    MessageBox.Show(this, "El DNI-RUC es requerido", "Mensaje", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    dniRucTextBox.Focus();
                }
            }
        }

        private void dniRucTextBox_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
        {
            if (e.KeyCode == Keys.Tab)
            {
                if (dniRucTextBox.Text.Length > 0)
                {
                    VerificarRuc();
                }
                else
                {
                    MessageBox.Show(this, "El DNI-RUC es requerido", "Mensaje", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    dniRucTextBox.Focus();
                }
            }
        }

        private void InhabilitarTextBoxs()
        {
            nombreRazonSocialTextBox.Enabled = false;
            direccionTextBox.Enabled = false;
            emailTextBox.Enabled = false;
            telefonoTextBox.Enabled = false;
        }

        private void HabilitarTextBoxs()
        {
            nombreRazonSocialTextBox.Enabled = true;
            direccionTextBox.Enabled = true;
            emailTextBox.Enabled = true;
            telefonoTextBox.Enabled = true;
        }


        private void dniRucTextBox_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!(char.IsNumber(e.KeyChar)) && (e.KeyChar != (char)Keys.Back) 
                && (e.KeyChar != (char)Keys.Enter) && (e.KeyChar != (char)Keys.Clear))
            {
                e.Handled = true;
                return;
            }
        }

        private void nombreRazonSocialTextBox_KeyPress(object sender, KeyPressEventArgs e)
        {
            var guion = Convert.ToString("-");
            var punto = Convert.ToString(".");
            var amberson = Convert.ToString("&");

            string cadena = guion + amberson + punto + (char)8;

            if (!cadena.Contains(e.KeyChar) && !(char.IsLetter(e.KeyChar)) && !(char.IsNumber(e.KeyChar))
                && (e.KeyChar != (char)Keys.Back) && (e.KeyChar != (char)Keys.Enter) && (e.KeyChar != (char)Keys.Clear)
                && !(char.IsSeparator(e.KeyChar)))
            {
                e.Handled = true;
                return;
            }
        }

        public int VecesNumeral = 0;
        public int VecesAsterisc = 0;
        private void telefonoTextBox_KeyPress(object sender, KeyPressEventArgs e)
        {
            var numeral = Convert.ToString("#");
            var asterisc = Convert.ToString("*");
            string cadena = numeral + asterisc + (char)8;

            if (numeral.Contains(e.KeyChar) || asterisc.Contains(e.KeyChar))
            {
                VecesNumeral++;

                if (VecesNumeral == 1 || VecesAsterisc == 1)
                {
                    if (!cadena.Contains(e.KeyChar) && !(char.IsNumber(e.KeyChar)) && (e.KeyChar != (char)Keys.Back)
                        && (e.KeyChar != (char)Keys.Enter) && (e.KeyChar != (char)Keys.Clear))
                    {
                        e.Handled = true; return;
                    }
                }
                else
                {
                    if (!(char.IsNumber(e.KeyChar)) && (e.KeyChar != (char)Keys.Back)
                        && (e.KeyChar != (char)Keys.Enter) && (e.KeyChar != (char)Keys.Clear))
                    {
                        e.Handled = true; return;
                    }
                }
            }
            else
            {
                if (!(char.IsNumber(e.KeyChar)) && (e.KeyChar != (char)Keys.Back)
                        && (e.KeyChar != (char)Keys.Enter) && (e.KeyChar != (char)Keys.Clear))
                {
                    e.Handled = true;
                    return;
                }
                VecesNumeral = 0;
            }

        }

        
        
   
    }
}
